package com.loe.mvp.ext_app

import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemChildLongClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.listener.OnItemLongClickListener
import com.loe.mvp.list.QuickAdapter
import org.json.JSONObject

/**
 * 列表
 */

fun RecyclerView.addOnItemClickListener(onClick: (i: Int) -> Unit)
{
    this.addOnItemTouchListener(object : OnItemClickListener()
    {
        override fun onSimpleItemClick(baseQuickAdapter: BaseQuickAdapter<*, *>?, view: View?, i: Int)
        {
            onClick(i)
        }
    })
}

/**
 * 添加延时、防重复点击的itemClick事件
 */
fun RecyclerView.addDelayItemClickListener(delay: Long = 100, interval: Long = 1200, onClick: (i: Int) -> Unit)
{
    var t = 0L
    this.addOnItemTouchListener(object : OnItemClickListener()
    {
        override fun onSimpleItemClick(baseQuickAdapter: BaseQuickAdapter<*, *>?, view: View?, i: Int)
        {
            if (System.currentTimeMillis() - t > interval)
            {
                postDelayed({ onClick(i) }, delay)
                t = System.currentTimeMillis()
            }
        }
    })
}

fun RecyclerView.addOnItemChildClickListener(id: Int = 0, onClick: (i: Int) -> Unit)
{
    this.addOnItemTouchListener(object : OnItemChildClickListener()
    {
        override fun onSimpleItemChildClick(baseQuickAdapter: BaseQuickAdapter<*, *>?, view: View?, i: Int)
        {
            if (id == 0 || view?.id == id)
            {
                onClick(i)
            }
        }
    })
}

fun RecyclerView.addOnItemChildViewClickListener(id: Int = 0, onClick: (i: Int, view: View) -> Unit)
{
    this.addOnItemTouchListener(object : OnItemChildClickListener()
    {
        override fun onSimpleItemChildClick(baseQuickAdapter: BaseQuickAdapter<*, *>?, view: View?, i: Int)
        {
            if (id == 0 || view?.id == id)
            {
                if(view != null)
                {
                    onClick(i, view)
                }
            }
        }
    })
}

fun RecyclerView.addOnItemChildClickListener(id: Int = 0, interval: Long, onClick: (i: Int) -> Unit)
{
    var t = 0L
    this.addOnItemTouchListener(object : OnItemChildClickListener()
    {
        override fun onSimpleItemChildClick(baseQuickAdapter: BaseQuickAdapter<*, *>?, view: View?, i: Int)
        {
            if (id == 0 || view?.id == id && System.currentTimeMillis() - t > interval)
            {
                onClick(i)
                t = System.currentTimeMillis()
            }
        }
    })
}

fun RecyclerView.addOnItemChildViewClickListener(id: Int = 0, interval: Long, onClick: (i: Int, view: View) -> Unit)
{
    var t = 0L
    this.addOnItemTouchListener(object : OnItemChildClickListener()
    {
        override fun onSimpleItemChildClick(baseQuickAdapter: BaseQuickAdapter<*, *>?, view: View?, i: Int)
        {
            if (id == 0 || view?.id == id && System.currentTimeMillis() - t > interval)
            {
                if(view != null)
                {
                    onClick(i, view)
                    t = System.currentTimeMillis()
                }
            }
        }
    })
}

fun RecyclerView.addOnItemLongClickListener(onClick: (i: Int) -> Unit)
{
    this.addOnItemTouchListener(object : OnItemLongClickListener()
    {
        override fun onSimpleItemLongClick(baseQuickAdapter: BaseQuickAdapter<*, *>?, view: View?, i: Int)
        {
            onClick(i)
        }
    })
}

fun RecyclerView.addOnItemChildLongClickListener(id: Int = 0, onClick: (i: Int) -> Unit)
{
    this.addOnItemTouchListener(object : OnItemChildLongClickListener()
    {
        override fun onSimpleItemChildLongClick(baseQuickAdapter: BaseQuickAdapter<*, *>?, view: View?, i: Int)
        {
            if (id == 0 || view?.id == id)
            {
                onClick(i)
            }
        }
    })
}

fun RecyclerView.addHeaderView(view: View)
{
    (adapter as BaseQuickAdapter<*, *>).addHeaderView(view)
}

fun RecyclerView.addHeaderView(view: View, index: Int)
{
    (adapter as BaseQuickAdapter<*, *>).addHeaderView(view, index)
}

fun RecyclerView.addFooterView(view: View)
{
    (adapter as BaseQuickAdapter<*, *>).addFooterView(view)
}

fun RecyclerView.addFooterView(view: View, index: Int)
{
    (adapter as BaseQuickAdapter<*, *>).addFooterView(view, index)
}

fun <T> createAdapter(@LayoutRes layoutResId: Int, list: List<T> = ArrayList(), convert: View.(holder: BaseViewHolder, bean: T) -> Unit): QuickAdapter<T>
{
    return object : QuickAdapter<T>(layoutResId, list)
    {
        override fun View.convert(holder: BaseViewHolder, bean: T)
        {
            convert(holder, bean)
        }
    }
}

fun <T> RecyclerView.setQuickAdapter(@LayoutRes layoutResId: Int, list: List<T> = ArrayList(), convert: View.(holder: BaseViewHolder, bean: T) -> Unit): QuickAdapter<T>
{
    adapter = createAdapter(layoutResId, list, convert)
    return adapter as QuickAdapter<T>
}

fun createJsonAdapter(@LayoutRes layoutResId: Int, list: List<JSONObject> = ArrayList(), convert: View.(holder: BaseViewHolder, json: JSONObject) -> Unit): QuickAdapter<JSONObject>
{
    return object : QuickAdapter<JSONObject>(layoutResId, list)
    {
        override fun View.convert(holder: BaseViewHolder, json: JSONObject)
        {
            convert(holder, json)
        }
    }
}

fun RecyclerView.setJsonAdapter(@LayoutRes layoutResId: Int, list: List<JSONObject> = ArrayList(), convert: View.(holder: BaseViewHolder, json: JSONObject) -> Unit): QuickAdapter<JSONObject>
{
    adapter = createJsonAdapter(layoutResId, list, convert)
    return adapter as QuickAdapter<JSONObject>
}

fun createStringAdapter(@LayoutRes layoutResId: Int, list: List<String> = ArrayList(), convert: View.(holder: BaseViewHolder, s: String) -> Unit): QuickAdapter<String>
{
    return object : QuickAdapter<String>(layoutResId, list)
    {
        override fun View.convert(holder: BaseViewHolder, s: String)
        {
            convert(holder, s)
        }
    }
}

fun RecyclerView.setStringAdapter(@LayoutRes layoutResId: Int, list: List<String> = ArrayList(), convert: View.(holder: BaseViewHolder, s: String) -> Unit): QuickAdapter<String>
{
    adapter = createStringAdapter(layoutResId, list, convert)
    return adapter as QuickAdapter<String>
}